package com.xwh.b2c.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xwh.b2c.common.PageResult;
import com.xwh.b2c.common.Result;
import com.xwh.b2c.dto.CategoryPageQueryDTO;
import com.xwh.b2c.entity.Category;
import com.xwh.b2c.entity.Member;
import com.xwh.b2c.service.CategoryService;
import com.xwh.b2c.vo.CategoryPageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 产品分类表 前端控制器
 * </p>
 *
 * @author xoox
 * @since 2025-07-10
 */
@Slf4j
@RestController
@RequestMapping("/category")
@Tag(name = "分类管理")
public class CategoryController {
    @Resource
    private CategoryService categoryService;
    @Operation(summary = "新增分类",description = "新增分类")
    @PostMapping("")
    public Result<Object> category(@RequestBody Category category){
        this.categoryService.save(category);
        return Result.success();
    }

    @Operation(summary = "查询分类列表")
    @GetMapping("list")
    public Result<List<Category>> getList(){
        List<Category> list = this.categoryService.list();
        return Result.success(list);
    }

    @Operation(summary = "按id查询")
    @GetMapping("{id}")
    public Result<Category> get(@PathVariable("id") Long id){
        Category category = this.categoryService.getById(id);
        return Result.success(category);
    }



    @Operation(summary = "分页查询")
    @GetMapping("/page")
    public PageResult<CategoryPageVO> page(@ModelAttribute CategoryPageQueryDTO categoryPageQueryDTO){
        IPage<CategoryPageVO> page = this.categoryService.getPage(categoryPageQueryDTO);

        log.info("pageVO:{}",page);
        return PageResult.success(page);
    }


    @Operation(summary = "修改分类信息")
    @PutMapping("update")
    public Result<Object> update(@RequestBody Category category){
        this.categoryService.updateById(category);
        return Result.success();
    }
    @Operation(summary = "删除分类")
    @DeleteMapping("delete")
    public Result<Object> delete(@RequestBody List<Long> ids){
        this.categoryService.removeBatchByIds(ids);
        return Result.success();
    }


    @Operation(summary = "可见/不可见设置", description = "根据ID 可见或隐藏分类")
    @PutMapping("enable/{id}")
    public Result<Object> enable(@PathVariable Long id){
        Category member = this.categoryService.getById(id);
        this.categoryService.enable(member);
        return Result.success();
    }

}
